﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class OrderObject
    {
        public static Order[] GetOrders()
        {
            return Query(null);
        }

        public static Order GetOrder(int id)
        {
            Order order = new Order();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(OrderDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = OrderDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable orders = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            order.Id              = Convert.ToInt32(orders.Rows[0][OrderDesc.Id]);
            order.BillId          = Convert.ToInt32(orders.Rows[0][OrderDesc.BillId]);
            order.ProductId       = Convert.ToInt32(orders.Rows[0][OrderDesc.ProductId]);
            order.Count           = Convert.ToInt32(orders.Rows[0][OrderDesc.Count]);
            order.Discount        = Convert.ToInt32(orders.Rows[0][OrderDesc.Discount]);
            order.Price           = Convert.ToInt32(orders.Rows[0][OrderDesc.Price]);
            order.BusinessState   = Convert.ToInt32(orders.Rows[0][OrderDesc.BusinessState]);
            order.EmployeeId      = Convert.ToInt32(orders.Rows[0][OrderDesc.EmployeeId]);
            order.Remark          = Convert.ToString(orders.Rows[0][OrderDesc.Remark]);

            return order;
        }

        public static Order[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + OrderDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                Order[] orders = new Order[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Order order = new Order();

                    order.Id                 = Convert.ToInt32(table.Rows[i][OrderDesc.Id]);
                    order.BillId             = Convert.ToInt32(table.Rows[i][OrderDesc.BillId]);
                    order.ProductId          = Convert.ToInt32(table.Rows[i][OrderDesc.ProductId]);
                    order.Count              = Convert.ToInt32(table.Rows[i][OrderDesc.Count]);
                    order.Discount           = Convert.ToInt32(table.Rows[i][OrderDesc.Discount]);
                    order.Price              = Convert.ToInt32(table.Rows[i][OrderDesc.Price]);
                    order.BusinessState      = Convert.ToInt32(table.Rows[i][OrderDesc.BusinessState]);
                    order.EmployeeId         = Convert.ToInt32(table.Rows[i][OrderDesc.EmployeeId]);
                    order.Remark             = Convert.ToString(table.Rows[i][OrderDesc.Remark]);



                    orders.SetValue(order, i);
                }

                return orders;
            }
            return null;
        }

        public static Int32 Add(Order order)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(OrderDesc.BillId,            order.BillId);
            builder.InsertAdd(OrderDesc.ProductId,         order.ProductId);
            builder.InsertAdd(OrderDesc.Count,             order.Count);
            builder.InsertAdd(OrderDesc.Discount,          order.Discount);
            builder.InsertAdd(OrderDesc.Price,             order.Price);
            builder.InsertAdd(OrderDesc.BusinessState,     order.BusinessState);
            builder.InsertAdd(OrderDesc.EmployeeId,        order.EmployeeId);
            builder.InsertAdd(OrderDesc.Remark,            order.Remark);

            builder.QueryTableName = OrderDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(OrderDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = OrderDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Order order)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(OrderDesc.BillId,            order.BillId);
            builder.UpdateAdd(OrderDesc.ProductId,         order.ProductId);
            builder.UpdateAdd(OrderDesc.Count,             order.Count);
            builder.UpdateAdd(OrderDesc.Discount,          order.Discount);
            builder.UpdateAdd(OrderDesc.Price,             order.Price);
            builder.UpdateAdd(OrderDesc.BusinessState,     order.BusinessState);
            builder.UpdateAdd(OrderDesc.EmployeeId,        order.EmployeeId);
            builder.UpdateAdd(OrderDesc.Remark,            order.Remark);
            builder.ConditionAdd(OrderDesc.Id, ConditionExpress.EQUALS, order.Id);
            builder.QueryTableName = OrderDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
